//
//  UIWebView+JavaScript.m
//  yunlehui
//
//  Created by saxon on 15/9/20.
//  Copyright (c) 2015年 saxon. All rights reserved.
//

#import "UIWebView+JavaScript.h"
#import <objc/runtime.h>
#import "RTAlertView.h"



@implementation UIWebView (JavaScript)
static BOOL diagStat = NO;
- (id)dicWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    //    NSLog(@"%@", jsonString);
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    id dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                             options:NSJSONReadingMutableLeaves
                                               error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}
-(void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame{
    
    
    NSDictionary *alertDic = [self dicWithJsonString:message];
    
    if (alertDic == nil && message) {
        RTAlertView *alert = [[RTAlertView alloc] initWithTitle:nil message:message cancelButtonTitle:@"确定" otherButtonTitles:nil  ReturnBlock:nil];
        [alert show];
    }
    if ([alertDic allKeys].count) {
        if (self.jsDelegate && [self.jsDelegate respondsToSelector:@selector(webView:runJavaScriptAlertMessage:)]) {
            [self.jsDelegate webView:sender runJavaScriptAlertMessage:alertDic];
        }
    }
}
/*!
 @method
 @bired  拨打电话
 */
- (void)callPhone:(NSString *)phone
{
    if (phone != nil && ![phone isEqualToString:@""]) {
        RTAlertView *alert = [[RTAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"呼叫:%@", phone] cancelButtonTitle:@"取消" otherButtonTitles:@"呼叫" ReturnBlock:^(int buttonIndex) {
            if(buttonIndex){
                NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phone]];
                UIApplication *app = [UIApplication sharedApplication];
                if ([app canOpenURL:url]) {
                    [app openURL:url];
                } else {
//                    [[UIApplication sharedApplication].windows.firstObject.rootViewController showHint:@"号码格式有误"];
                }
            }
        }];
        [alert show];
    }
}
-(BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame{
    
    RTAlertView* dialogue = [[RTAlertView alloc] initWithTitle:nil message:message cancelButtonTitle:@"确定" otherButtonTitles:@"取消" ReturnBlock:^(int buttonIndex) {
        if (buttonIndex == 0) {
            diagStat = YES;
        }else if(buttonIndex==1){
            diagStat = NO;
        }
    }];
    [dialogue show];
    return diagStat;
}
- (void)setJsDelegate:(id<UIWebViewJavaScriptDelegate>)jsDelegate
{
    objc_setAssociatedObject(self, @selector(property), jsDelegate, OBJC_ASSOCIATION_ASSIGN);
}
- (id<UIWebViewJavaScriptDelegate>)jsDelegate
{
    return objc_getAssociatedObject(self, @selector(property));
}
@end
